####################################################################################################
### Title: People Consistently View Elections and Civil Liberties as Key Components of Democracy ###
### Content: Cleaning the Japanese dataset                                                       ###
### Date: August 24, 2024                                                                        ###
####################################################################################################

### Set-up ----
## Clean the working environment and set the working directory
rm(list = ls())
setwd("~/Desktop/Science_Replication/data_cleaning")

## Install the cjdata package (if not yet installed)
# library(devtools)  # version 2.4.3
# install_github(repo = "yhoriuchi/cjdata")

## Load the required packages
library(tidyverse) # version 2.0.0
library(cjdata)    # version 0.1.0

## Read the raw dataset
df <- read_Qualtrics("raw_JP.csv")

### Data cleaning ----
## Recode variables of respondent characteristics
# Minority status (1 = minority; 0 = otherwise)
df <- df %>% mutate(minority_bin = case_when(
  minority == "はい" ~ 1,
  minority == "いいえ" | minority == "わからない" ~ 0
))
df$minority_bin <- factor(df$minority_bin, 0:1, c("Non-Minority", "Minority"))
table(df$minority_bin)

# Gender (1 = female; 0 = male)
df <- df %>% rename(gender5 = gender)
df <- df %>% mutate(gender_bin = case_when(
  gender5 == "女性" ~ 1,
  gender5 == "男性" ~ 0
))
df$gender_bin <- factor(df$gender_bin, 0:1, c("Male", "Female"))
table(df$gender_bin)

# Self-reported political ideology (1 = right; 0 = left)
df <- df %>% mutate(ideo_bin = case_when(
  political == "非常に左翼" |
    political == "適度に左翼" |
    political == "やや左翼" ~ 0,
  political == "非常に右翼" |
    political == "適度に右翼" |
    political == "やや右翼" ~ 1
))
df$ideo_bin <- factor(df$ideo_bin, 0:1, c("Left", "Right"))
table(df$ideo_bin)

# Education (1 = college; 0 = no college)
df <- df %>% mutate(edu_bin = case_when(
  edu == "中学校卒業以下" |
    edu == "高校卒業または同程度の学歴" |
    edu == "短期大学士" |
    edu == "一部の大学、学位なし" ~ 0,
  edu == "学士号／4年制学士" |
    edu == "上級学位（修士号、博士号、法務博士など）" ~ 1
))
df$edu_bin <- factor(df$edu_bin, 0:1, c("No College", "College"))
table(df$edu_bin)

# Geopolitical alignment (1 = pro-China; 0 = pro-US)
df <- df %>% mutate(pro_china = case_when(
  china_us1 == "多分米国になって欲しい" |
    china_us1 == "必ず米国になって欲しい" ~ 0,
  china_us1 == "多分中国になって欲しい" |
    china_us1 == "必ず中国になって欲しい" ~ 1
))
df$pro_china <- factor(df$pro_china, 0:1 , c("Pro-US", "Pro-China"))
table(df$pro_china)

## Subset and reshape dataset into wide format that each row is a distinct conjoint task
df$Q1.1 <- ifelse(df$Q1.1 == "国A", "Country A", "Country B")
df$Q1.5 <- ifelse(df$Q1.5 == "国A", "Country A", "Country B")
df$Q1.9 <- ifelse(df$Q1.9 == "国A", "Country A", "Country B")
df_cj <- cjdata::reshape_conjoint(df, ResponseId, paste0("Q1.", seq(from = 1, to = 9, by = 4)))

## Rename the variables
df_cj <- df_cj %>% 
  rename(econ = "裕福な人たちと貧乏な人たちの経済状況は…",
         populist = "政策策定時に、政治家は…",
         direct = "人々が政策決定に直接投票する頻度は…",
         civil = "言論・宗教・集会の自由などの市民の個人の自由は…",
         gender = "男性と女性の権利は…",
         expert = "選挙で選出されていない独立した専門家が…",
         obedient = "市民が政府の政策を気に入らない場合…",
         election = "この国の政治指導者の選挙は…",
         leader = "決定を下すとき、この国の指導者は…")

## Reorder the factors
# Electoral democracy
df_cj$election <- 
  factor(df_cj$election, 
         levels = c("行われていないまたは存在しない", "現在権力を保持している政治団体に有利に偏っている", "さまざまな政治団体によって自由かつ公正に争われている"), 
         labels = c("Elections are not held", "Elections are biased", "Elections are free and fair"))

# Liberal democracy
df_cj$civil <- 
  factor(df_cj$civil, 
         levels = c("法律によってまったく保護されていない", "法律によってあまり保護されていない", "法律によって強く保護されている"), 
         labels = c("Civil liberties are not at all protected", "Civil liberties are weakly protected", "Civil liberties are strongly protected"))

# Institutional democracy
df_cj$leader <- 
  factor(df_cj$leader, 
         levels = c("ほとんどの場合、立法府と裁判所の権限を回避できる", "立法府と裁判所の権限を回避できる場合もある", "立法府と裁判所の権限を尊重しなければならない"), 
         labels = c("Leader is weakly constrained", "Leader is somewhat constrained", "Leader is highly constrained"))

# Populist democracy
df_cj$populist <- 
  factor(df_cj$populist, 
         levels = c("多数派の希望にほとんど従わない", "多数派の希望に従う場合もある", "多数派の希望に頻繁に従う"), 
         labels = c("Leader rarely follows the majority", "Leader sometimes follows the majority", "Leader frequently follows the majority"))

# Loyalist democracy
df_cj$obedient <- 
  factor(df_cj$obedient, 
         levels = c("ほとんどの人たちが政府に従う", "政府に従う人たちもいれば異議を唱える人たちもいる", "ほとんどの人たちが政府に異議を唱える"), 
         labels = c("Dissidents mostly obey the gov't", "Dissidents occasionally obey the gov't", "Dissidents mostly challenge the gov't"))

# Substantive democracy (economy)
df_cj$econ <- 
  factor(df_cj$econ, 
         levels = c("非常に不平等", "やや不平等", "平等"), 
         labels = c("Economic equality is very low", "Economic equality is somewhat low", "Economic equality is high"))

# Substantive democracy (gender)
df_cj$gender <- 
  factor(df_cj$gender, 
         levels = c("非常に不平等", "やや不平等", "平等"), 
         labels = c("Gender equality is very low", "Gender equality is somewhat low", "Gender equality is high"))

# Technocratic democracy
df_cj$expert <- 
  factor(df_cj$expert, 
         levels = c("政策にそれほど影響を与えない", "政策にある程度影響を与える", "政策に大きな影響を与える"), 
         labels = c("Experts have small influence on policy", "Experts have some influence on policy", "Experts have much influence on policy"))

# Direct democracy
df_cj$direct <- 
  factor(df_cj$direct, 
         levels = c("まれ", "時々", "頻繁"),
         labels = c("Policies are rarely voted on", "Policies are sometimes voted on", "Policies are frequently voted on"))

## Incorporate respondent IDs and other covariates
df <- df %>% rename(id = ResponseId)
df_cj <- merge(df_cj, df, by = "id")

## Indicate the country
df_cj$country <- "JP"

## Export the dataset
write.csv(df_cj, "df_JP.csv", row.names = FALSE)
